#import "ALSourceListAppView.h"


#pragma mark Constants
static NSString* const ALSourceListAppViewTitleField = @"ALSourceListAppViewTitleField";
static NSString* const ALSourceListAppViewVersionField = @"ALSourceListAppViewVersionField";
static NSString* const ALSourceListAppViewDisclosureButton = @"ALSourceListAppViewDisclosureButton";
static CGFloat const ALSourceListAppViewHeaderHeight = 58.0;


#pragma mark -
#pragma mark Class globals
static NSGradient *ALHighlightedBackgroundGradient;
static NSColor *ALSourceListViewTopBorderColor;
static NSColor *ALSourceListViewBottomBorderColor;


#pragma mark -
@implementation ALSourceListAppView

@synthesize titleTextField;
@synthesize versionTextField;
@synthesize highlighted;


#pragma mark -
#pragma mark Initialization

+(void)initialize {
	NSColor *startColor = [NSColor colorWithCalibratedRed:0.145 green:0.420 blue:0.718 alpha:1.000];
	NSColor *endColor = [NSColor colorWithCalibratedRed:0.431 green:0.647 blue:0.855 alpha:1.000];
	ALHighlightedBackgroundGradient = [[NSGradient alloc] initWithStartingColor:startColor endingColor:endColor];
	ALSourceListViewTopBorderColor = [NSColor colorWithCalibratedRed:0.314 green:0.532 blue:0.758 alpha:1.000];
	ALSourceListViewBottomBorderColor = [NSColor colorWithCalibratedRed:0.220 green:0.373 blue:0.530 alpha:1.000];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self setTitleTextField:[aDecoder decodeObjectForKey:ALSourceListAppViewTitleField]];
	[self setVersionTextField:[aDecoder decodeObjectForKey:ALSourceListAppViewVersionField]];
	
	return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
	[super encodeWithCoder:aCoder];
	[aCoder encodeObject:[self titleTextField] forKey:ALSourceListAppViewTitleField];
	[aCoder encodeObject:[self versionTextField] forKey:ALSourceListAppViewVersionField];
}


#pragma mark -
#pragma mark Highlighting

-(void)setHighlighted:(BOOL)shouldHighlight {
	highlighted = shouldHighlight;
	[[[self titleTextField] cell] setHighlighted:shouldHighlight];
	[[[self versionTextField] cell] setHighlighted:shouldHighlight];
	[self setNeedsDisplay:YES];
}

-(NSGradient *)highlightedGradient {
	return ALHighlightedBackgroundGradient;
}

-(NSColor *)topBorderColor {
	return ALSourceListViewTopBorderColor;
}

-(NSColor *)bottomBorderColor {
	return ALSourceListViewBottomBorderColor;
}


#pragma mark -
#pragma mark Drawing

- (void)drawRect:(NSRect)dirtyRect {
	if (![self highlighted]) 
		return;
	NSRect bounds = [self bounds];

	CGFloat left = NSMinX(bounds);
	CGFloat right = NSMaxX(bounds);
	CGFloat top = NSMaxY(bounds);
	CGFloat bottom = NSMaxY(bounds) - ALSourceListAppViewHeaderHeight;

	NSPoint topLeft = NSMakePoint(left, top);
	NSPoint topRight = NSMakePoint(right, top);
	NSPoint bottomLeft = NSMakePoint(left, bottom);
	NSPoint bottomRight = NSMakePoint(right, bottom);
	NSRect headerRect = NSMakeRect(left, bottom, right - left, top - bottom);
	
	[[self highlightedGradient] drawInRect:headerRect angle:90];
	[[self topBorderColor] setStroke];
	[NSBezierPath strokeLineFromPoint:topLeft toPoint:topRight];
	[[self bottomBorderColor] setStroke];
	[NSBezierPath strokeLineFromPoint:bottomLeft toPoint:bottomRight];
}

@end
